# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
_ver_base=1.6
pkgver=1.6.1644.9b7a1cc
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ilmbase"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "git")
source=("${_realname}"::"git+https://github.com/imageworks/OpenShadingLanguage.git"
        0001-osolex-Define-isatty-as-_isatty-for-_WIN32.patch
        0002-Use-getcwd-for-older-OIIO.patch
        0003-Fix-unsigned-signed-compares.patch
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0005-Fix-cast-from-void-ptr.patch
        0007-Fix-bad-casts-with-intptr_t.patch)
sha256sums=('SKIP'
            'b6b6ecb22d6d317d9a9ad495f4b8a0d57ff7deab12cecd16c4abcd278446cfc4'
            '1e8669c1ccc298c7871bfe51f1d77284447fd78eb588c0389fabeb5e79d47c18'
            'd86c235bce81b9931fea805dcb135647b1d47986be7046e23ecdeec094feca04'
            'c9e02624cf6e607118f1f7fd8bf8bda9af5103afde5770a11fa5ccf49916fc5b'
            '597241b0d10799b9d38629f64a98c2cc54e48dbbf9b893fdd078cb40da019240'
            '541cc1eb0c506e253de2f26af37dc6e35de891a940906c6e33a2f4774a4a844e'
            'd3e6d2bdd059ec9b1581b02c9f736f26a06e1189929c6276d1d7d81204bb3535')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
  git am "${srcdir}"/0001-osolex-Define-isatty-as-_isatty-for-_WIN32.patch
  git am "${srcdir}"/0002-Use-getcwd-for-older-OIIO.patch
  git am "${srcdir}"/0003-Fix-unsigned-signed-compares.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
}

build() {
  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  local _extra_libs=$(${MINGW_PREFIX}/bin/llvm-config --system-libs)
  CFLAGS+=" -std=c++11 -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -std=c++11 -DTINYFORMAT_ALLOW_WCHAR_STRINGS -D_USE_MATH_DEFINES"

  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DILMBASE_CUSTOM=True \
    -DILMBASE_CUSTOM_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DILMBASE_CUSTOM_LIB_DIR=${MINGW_PREFIX}/lib \
    -DILMBASE_CUSTOM_LIBRARIES="Half Iex Imath IlmThread" \
    -DEXTRA_OSLEXEC_LIBRARIES="${_extra_libs}" \
    -DLLVM_STATIC=1 \
    -DOSL_BUILD_CPP11=1 \
    ../$_realname

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH="$PWD/src/liboslcomp:${PATH}" make
}

package() {
  cd build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  mv "${pkgdir}${MINGW_PREFIX}"/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname} # TODO: Tell upstream about this shit
  mv "${pkgdir}${MINGW_PREFIX}"/{CHANGES,README.md,INSTALL} "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mv "${pkgdir}${MINGW_PREFIX}"/doc "${pkgdir}${MINGW_PREFIX}"/share/OSL/doc
}
